﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using ComLib.ComFun;

namespace CoreBroker
{
    public class WindowEmbedder
    {
        public const int GWL_STYLE = (-16);

        public const int WS_VISIBLE = 0x10000000;

        public IntPtr MainWindowHandle
        {
            private set;
            get;
        }

        [DllImport("user32.dll", SetLastError = true)]
        public static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

        [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);

        public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
        {
            if (IntPtr.Size == 4)
            {
                return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
            }
            return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
        }

        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, int dwNewLong);
     
        private IntPtr GetProcessMainWnd(string windowName, string className)
        {
            IntPtr[] FindWindows = SearchWindow.Search(windowName, className);
            if (FindWindows.Length > 0)
            {
                IntPtr MainWnd = FindWindows[0];
                //string strLog = string.Format("查询目标窗口句柄{0}", MainWnd);
                //Log.WriteAsync(strLog);
                return MainWnd;
            }
            return IntPtr.Zero;
        }

        /// <summary>  
        /// 将指定的程序嵌入指定的控件  
        /// </summary>  
        public void EmbedProcess(Process Proc, Control ParentContainer)
        {
            // 获取主窗体句柄  
            if (Proc == null || ParentContainer == null)
                return;
            MainWindowHandle = Proc.MainWindowHandle;
            if (Proc.MainWindowHandle == IntPtr.Zero)
            {
                MainWindowHandle = GetProcessMainWnd(Proc.ProcessName, Proc.ProcessName);
            }
            try
            {
                WinAPI.ShowWindow(MainWindowHandle, (int)ShowCommands.SW_SHOW);

                // 将主窗体嵌入到给定容器控件  
                SetParent(MainWindowHandle, ParentContainer.Handle);
                
                // 移动主窗体到给定容器控件大小 
                MoveWindow(MainWindowHandle, 0, 0, ParentContainer.Width, ParentContainer.Height, true);
                
                // 隐藏边框                 
                SetWindowLong(new HandleRef(this, MainWindowHandle), GWL_STYLE, WS_VISIBLE);
                
                //设置窗体标题
                //if (WindowTitle != "")
                //{
                //    SendMessage(Proc.MainWindowHandle, WM_SETTEXT, IntPtr.Zero, WindowTitle);
                //}

                ParentContainer.Refresh();
            }
            catch
            {
            }
        }
    }
}
